<!--
 * @Author: your name
 * @Date: 2021-06-22 14:46:34
 * @LastEditTime: 2021-06-28 09:22:55
 * @LastEditors: Please set LastEditors
 * @Description: 资讯发布页面
 * @FilePath: \briup\day07\vue-admin-template\src\pages\product\List.vue
-->
<template>
  <div>
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="60px"
      size="medium"
    >
      <el-row>
        <el-col :span="17">
          <el-form-item label="标题" prop="title">
            <el-input v-model="ruleForm.title" style="width: 800px" />
          </el-form-item>
          <el-form-item label="分类" prop="categoryId">
            <el-select
              v-model="ruleForm.categoryId"
              placeholder="请选择"
              clearable
            >
              <el-option
                v-for="item in categoryData"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item label="封皮" prop="cover">
            <el-upload
              class="avatar-uploader"
              :action="uploadFileURL"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" />
            </el-upload>
            <div style="font-size: 12px; color: #606266">
              文件大小不允许超过3M
            </div>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="正文" prop="content" style="margin-top: 40px">
        <div><tinymce v-model="ruleForm.content" :height="300" /></div>
      </el-form-item>
      <el-form-item style="margin-top: 30px; text-align: right">
        <el-button size="medium" @click="$router.back()">返回</el-button>
        <el-button
          size="medium"
          type="primary"
          @click="toRelease"
        >发布</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import { showFileURL, uploadFileURL } from '@/utils/config'
import { findAllcategory } from '@/api/infoCategory'
import { articlesaveOrUpdate } from '@/api/article'
export default {
  components: { Tinymce },
  data() {
    return {
      ruleForm: {
        title: '',
        categoryId: '',
        content: '',
        cover: ''
      },
      rules: {
        title: [{ required: true, message: '请输入文章标题', trigger: 'blur' }],
        categoryId: [
          { required: true, message: '请选择栏目', trigger: 'change' }
        ],
        content: [
          { required: true, message: '请输入文章内容', trigger: 'blur' }
        ],
        cover: [{ required: true, message: '请上传封皮', trigger: 'change' }]
      },
      categoryData: [],
      // 图片url
      imageUrl: '',
      showFileURL,
      uploadFileURL
    }
  },
  computed: {},
  created() {
    this.findcategoryData()
  },
  mounted() {},
  methods: {
    // 查询栏目分类
    async findcategoryData() {
      const res = await findAllcategory()
      this.categoryData = res.data
    },
    toRelease() {
      // 获取dom的值，对值进行校验
      this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          // 校验通过，做自己的业务
          const res = await articlesaveOrUpdate(this.ruleForm)
          // 提示用户保存成功，刷新表格数据，关闭模态框
          this.$notify.success({
            title: '成功',
            message: res.message
          })
          this.$router.back()
        } else {
          // 校验不通过
          return false
        }
      })
    },
    // 图片上传成功的回调函数
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.ruleForm.cover = showFileURL + res.data.id
    },
    // 图片上传之前的回调函数
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt3M = file.size / 1024 / 1024 < 3
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt3M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt3M
    }
  }
}
</script>
<style scoped>
</style>

